﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using YFIN.View.Controls;
using YFIN.View.CourseServiceRef;
using YFIN.View.Lockbox;
using YFIN.View.MarketTrend;
using YFIN.View.NewsAndCourse;
using YFIN.View.Toolkit;
using YFIN.View.User;
using YFIN.View.UserServiceRef;

namespace YFIN.View
{

    public partial class Stage : UserControl
    {
        public ICommand CloseTabCommand { get; set; }
        
        public static ModelUser User { get; private set; }
        private static Stage _stage;

        public Stage()
        {
            InitializeComponent();

            _stage = this;
            tabItem1.Content = new UserControl();
            tabItem1.Visibility = Visibility.Collapsed;
            tabItem2.Content = new MarketTrendView();
            tabItem2.Visibility = Visibility.Collapsed;
            tabItem3.Content = new UserControl();
            tabItem3.Visibility = Visibility.Collapsed;
            tabItem4.Content = new CourseView();
            tabItem4.Visibility = Visibility.Collapsed;
            tabItem5.Content = new NewsView();
            tabItem5.Visibility = Visibility.Visible;
            tabItem6.Content = new UserControl();
            tabItem6.Visibility = Visibility.Collapsed;
            tabControl1.SelectedItem = tabItem0;
        }

        public static void Login(ModelUser user)
        {
            User = user;
            _stage.logout.Visibility = Visibility.Visible;
            _stage.login.Visibility = Visibility.Collapsed;
            _stage.register.Visibility = Visibility.Collapsed;
            _stage.tb.Visibility = Visibility.Collapsed;

            _stage.tabItem1.Content = new ToolkitView();
            _stage.tabItem1.Visibility = Visibility.Visible;
            _stage.tabItem2.Visibility = Visibility.Visible;
            _stage.tabItem3.Content = new LockboxView();
            _stage.tabItem3.Visibility = Visibility.Visible;
            _stage.tabItem4.Visibility = Visibility.Visible;
            _stage.tabItem6.Content = new UserView();
            _stage.tabItem6.Visibility = Visibility.Visible;
        }

        public static void Logout()
        {
            _stage.tabControl1.SelectedItem = _stage.tabItem0;
            User = null;

            _stage.tabItem1.Content = new UserControl();
            _stage.tabItem1.Visibility = Visibility.Collapsed;
            _stage.tabItem2.Visibility = Visibility.Collapsed;
            _stage.tabItem3.Content = new UserControl();
            _stage.tabItem3.Visibility = Visibility.Collapsed;
            _stage.tabItem4.Visibility = Visibility.Collapsed;
            _stage.tabItem6.Content = new UserControl();
            _stage.tabItem6.Visibility = Visibility.Collapsed;

            _stage.logout.Visibility = Visibility.Collapsed;
            _stage.login.Visibility = Visibility.Visible;
            _stage.register.Visibility = Visibility.Visible;
            _stage.tb.Visibility = Visibility.Visible;

            ResetNewMsg();
        }

        private void news_Click(object sender, RoutedEventArgs e)
        {
            tabControl1.SelectedItem = tabItem6;
            ((UserView) tabItem6.Content).tabControl1.SelectedItem = ((UserView) tabItem6.Content).tabItem2;
        }

        private void logout_Click(object sender, RoutedEventArgs e)
        {
            Logout();
        }

        public static Login TheLogin { get; private set; }
        private void login_Click(object sender, RoutedEventArgs e)
        {
            TheLogin = new Login();
            TheLogin.Show();
        }

        public static Register Register { get; private set; }
        private void register_Click(object sender, RoutedEventArgs e)
        {
            Register = new Register();
            Register.Show();
        }

        public static void FullScreen()
        {
            _stage.Width = double.NaN;
            _stage.Content.Visibility = Visibility.Collapsed;
            _stage.title.Visibility = Visibility.Collapsed;
            _stage.btnPlayer.Visibility = Visibility.Collapsed;
            _stage.MediaPlayer.Margin = new Thickness(0, 0, 0, 0);
            _stage.MediaPlayer.Width = double.NaN;
            _stage.MediaPlayer.Height = double.NaN;
            _stage.mp.Margin = new Thickness(0, 0, 0, 0);
            _stage.mp.Width = double.NaN;
            _stage.mp.Height = double.NaN;
            if(!_stage._isRight)
            {
                _stage.StoryboardMpScaleBack.Begin();
            }
        }

        public static void UnFullScreen()
        {
            _stage.Content.Visibility = Visibility.Visible;
            _stage.title.Visibility = Visibility.Visible;
            _stage.btnPlayer.Visibility = Visibility.Visible;
            _stage.MediaPlayer.Margin = new Thickness(50, 60, 0, 0);
            _stage.MediaPlayer.Width = 850;
            _stage.MediaPlayer.Height = 500;
            _stage.mp.Margin = new Thickness(450, 110, 0, 0);
            _stage.mp.Width = 400;
            _stage.mp.Height = 300;
            if (!_stage._isRight)
            {
                _stage.StoryboardMpScale.Begin();
            }
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Width == 960)
            {
                UnFullScreen();
            }
        }

        private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_stage != null && _stage.MediaPlayer != null)
            {
                _stage.MediaPlayer.Visibility = ((TabControl)sender).SelectedIndex == 4 ? Visibility.Visible : Visibility.Collapsed;
                if(((TabControl)sender).SelectedIndex != 4)
                {
                    _stage.mp.DataContext = null;
                }
            }
        }

        public static void SetMediaSource(ModelCourse source)
        {
            _stage.mp.DataContext = source;
            _stage.title.Text = source.Name;
            _stage.keyWord.Text = source.KeyWord;
            _stage.summary.Text = source.Summary;
        }

        public static void SetNewMsg()
        {
            _stage.newMsg.Visibility = Visibility.Visible;
        }

        public static void ResetNewMsg()
        {
            _stage.newMsg.Visibility = Visibility.Collapsed;
        }

        private void btnPlayer_MouseEnter(object sender, MouseEventArgs e)
        {
            btnPlayer.Opacity = 0.9;
        }

        private void btnPlayer_MouseLeave(object sender, MouseEventArgs e)
        {
            btnPlayer.Opacity = 0.7;
        }

        private bool _isRight = true;
        private void btnPlayer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if(_isRight)
            {
                btnPlayer.Source = new BitmapImage(new Uri("/YFIN.View;component/Images/play.png", UriKind.RelativeOrAbsolute));
                StoryboardMediaPlayer.Begin();
                _isRight = false;
            }
            else
            {
                btnPlayer.Source = new BitmapImage(new Uri("/YFIN.View;component/Images/play2.png", UriKind.RelativeOrAbsolute));
                StoryboardMediaPlayerBack.Begin();
                _isRight = true;
            }
        }
    }
}
